package com.jiayi.weiphoto.page;

import com.jiayi.weiphoto.util.MyLog;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class YPSmartImageView extends ImageView{
	
//	public static final String TAG = "SmartImageView";
	
	private int reqWidth = -1;
	private int reqHeight = -1;

	public YPSmartImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public YPSmartImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public YPSmartImageView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public void setIntentHeight(int height){
		reqHeight = height;
		reqWidth = -1;
	}
	
	public void setIntentWidth(int width){
		reqWidth = width;
		reqHeight = -1;
	}
	boolean firstAdapt = false;
	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		// TODO Auto-generated method stub
		super.onLayout(changed, left, top, right, bottom);
		MyLog.e("onLayout left is "+left+"  top is "+top+" right is "+right+"  bottom is "+bottom);
		float scale = 1;
		Matrix matrix = new Matrix();
		if(getDrawable() != null && !firstAdapt){
			int drawableHeight = getDrawable().getIntrinsicHeight();
			float reqHeight =  (float)((View)getParent()).getMeasuredHeight();
			scale =reqHeight / drawableHeight;
//			setMinimumWidth((int) (right*scale));
			setLayoutParams(new RelativeLayout.LayoutParams((int) (right*scale), (int)(reqHeight)));
			MyLog.e("YP width is "+(int) (right*scale)+" and height is "+bottom);
			firstAdapt = true;
			invalidate();
		}
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

//	firstAdapt = false;
	@Override
	public void setImageBitmap(Bitmap bm) {
		// TODO Auto-generated method stub
		super.setImageBitmap(bm);
		requestLayout();
	}
	
}
